﻿package com.sms.monitor.utils;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

import android.text.TextUtils;

/**
 * 短信操作通用工具。
 * 
 *
 */
public class Util {

	/**
	 * 判断手机号码是否在设置拦截的号码集合中
	 * @param phoneSet
	 * @param phone
	 * @return
	 */
	public static boolean containsPhone(Set<String> phoneSet, String phone) {
		if (phoneSet != null && phoneSet.size() > 0) {
			Iterator<String> phones = phoneSet.iterator();
			while (phones.hasNext()) {
				String myPhone = phones.next();
				if (phone.equals(myPhone)) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 将字符串转换为字符集合
	 */
	public static Set<String> converStringToSet(String s) {
		Set<String> phoneSet = new TreeSet<String>();
		if (s != null && s.contains(",")) {
			String[] phones = s.split(",");
			for (int i = 0; i < phones.length; i++) {
				if (!TextUtils.isEmpty(phones[i])) {
					phoneSet.add(phones[i]);
				}
			}
		}
		return phoneSet;
	}

	/**
	 * 将字符集合转换为字符串
	 * @param phonesSet
	 * @return
	 */
	public static String converSetToString(Set<String> phonesSet) {
		StringBuilder builder = new StringBuilder();
		if (phonesSet != null && phonesSet.size() > 0) {
			Iterator<String> phones = phonesSet.iterator();
			while (phones.hasNext()) {
				String myPhone = phones.next();
				builder.append(myPhone).append(",");
			}
		}
		return builder.toString();
	}

	/**
	 * 将文本转换为链接
	 * @param keyword
	 * @return
	 */
	public static String converTextToHref(String keyword) {
		StringBuilder builder = new StringBuilder();
		builder.append("<a href=\"").append("http://m.baidu.com/s?word=").append(keyword).append("\">").append(keyword)
				.append("</a>");
		return builder.toString();
	}

	/**
	 * 替换短信内容中的关键字为链接
	 * @param content
	 * @param keywordSet
	 * @return
	 */
	public static String replaceAllKeyWords(String content, Set<String> keywordSet) {
		if (!TextUtils.isEmpty(content) && keywordSet != null && keywordSet.size() > 0) {
			Iterator<String> keywords = keywordSet.iterator();
			while (keywords.hasNext()) {
				String keyword = keywords.next();
				content = content.replaceAll(keyword, converTextToHref(keyword));

			}
		}
		return content;
	}
	
}